clear all
capture log close
capture set virtual off
capture set matsize 10000
capture set maxvar 30000

capture set more off
capture virtual off

pause on

/* This file generates the tables in the paper
Change the next line to replicate these results:
Point Stata to wherever you have the data file.
*/
cd "...\analysis_clean"
use main, clear

***************************************
*** TABLE 1: Treatment effects ********
***************************************
//Treatment effects on the log unit cost (yuan/kWh), quantity (kWh) and interactions with management scores
 
* Col 1
reg log_price_month i.treatment_t i.month i.firm_code t, robust cluster(firm_code)
eststo m1 
* Col 2
reg log_total_month i.treatment_t i.month i.firm_code t, robust cluster(firm_code)
eststo m2

// Test heterogenous effects
//generate interaction term of treatment*after treatment*mgmt_zscore: treatment_t_mgmt_z
gen treatment_t_mgmt_z = treatment_t * mgmt_zscore

* Col 3
reg log_price_month i.treatment_t treatment_t_mgmt_z i.firm_code i.month t, robust cluster(firm_code)
eststo m3
* Col 4
reg log_total_month i.treatment_t treatment_t_mgmt_z i.firm_code i.month t, robust cluster(firm_code)
eststo m4

centile(mgmt_score), centile(25, 50, 75)
gen mgmt_pct_25 = r(c_1)
gen mgmt_pct_50 = r(c_2)
gen mgmt_pct_75 = r(c_3)

//interaction of the bottom/top management quartile and treatment effects
gen mgmtp25_dummy = 0
replace mgmtp25_dummy = 1 if mgmt_score < mgmt_pct_25
gen mgmtp25_dummy_t = treatment_t * mgmtp25_dummy

gen mgmtp75_dummy = 0
replace mgmtp75_dummy = 1 if mgmt_score > mgmt_pct_75
gen mgmtp75_dummy_t = treatment_t * mgmtp75_dummy
* Col 5
reg log_price_month i.treatment_t i.mgmtp25_dummy_t i.firm_code i.month t, robust cluster(firm_code)
eststo m5
* Col 6
reg log_price_month i.treatment_t i.mgmtp75_dummy_t i.firm_code i.month t, robust cluster(firm_code)
eststo m6

//the effect of the adoption of transformer-related recommendations
//tariff_adopted: transformer adoption dummy that equals 1 for firms that adopted the transformer-related recommendation and zero otherwise.
gen tariff_adopted = 0

replace tariff_adopted = 1 if (firm_code == 1013 | firm_code == 1021 | firm_code == 1028 | firm_code == 1045 | firm_code == 1061 | firm_code == 1133 | firm_code == 4003 | firm_code == 4004 | firm_code == 7001 | firm_code == 8001 | firm_code == 8006)
gen tariff_t = treatment_t * tariff_adopted
* Col 7
reg log_price_month i.treatment_t tariff_t i.firm_code i.month t, robust cluster(firm_code)
eststo m7

//LATE
* Col 8
ivregress 2sls log_price_month i.month i.firm_code t (i.tariff_t=i.treatment_t), robust cluster(firm_code)  first
eststo m8 
estat firststage


***************************************
*** TABLE 2: Descriptive statistics ***
***************************************
use sample, clear

//calculate scope2 emissions
gen total_2015_MWh=newtotal_2015*1000 //MWh
gen emissions_2015=total_2015_MWh*1.0416  //tCO2,  emission factor=1.0416 in 2015
label variable emissions_2015 "Scope 2 emissions(tCO2)"
drop total_2015_MWh

* Col 1--full--91
asdoc sum newrevenue_2015 sales_15_13 newtotal_2015  elec_intensity  unit_cost emissions_2015 mgmt_score egy_mgmt_score,statistics( mean sd ) col(stat) format(%9.2f)

* Col 2---48
asdoc sum newrevenue_2015 sales_15_13 newtotal_2015  elec_intensity  unit_cost emissions_2015 mgmt_score egy_mgmt_score if treatment!=.,statistics( mean sd ) col(stat) format(%9.2f)

* Col 3-----ttest:48 vs 91  
gen fullsample=1
label var fullsample "91firms"
foreach v in newrevenue_2015 sales_15_13 newtotal_2015  elec_intensity  unit_cost emissions_2015 mgmt_score egy_mgmt_score  {
gen one_`v'=`v' if  fullsample==1
gen two_`v'=`v' if  treatment!=.
ttest one_`v'  =two_`v',  unpaired  
}
drop one_* two_*

* Col 4----treatment--24
asdoc sum newrevenue_2015 sales_15_13 newtotal_2015  elec_intensity  unit_cost emissions_2015 mgmt_score egy_mgmt_score if treatment==1,stat(mean sd) col(stat) append

* Col 5---control--24
asdoc sum newrevenue_2015 sales_15_13 newtotal_2015  elec_intensity  unit_cost emissions_2015 mgmt_score egy_mgmt_score if treatment==0,stat(mean sd) col(stat) append

* Col 6----ttest:treatment vs control
ttest newrevenue_2015, by(treatment)
ttest  sales_15_13, by(treatment)
ttest  newtotal_2015, by(treatment)
ttest  elec_intensity, by(treatment)
ttest  unit_cost, by(treatment)
ttest  emissions_2015, by(treatment)
ttest mgmt_score, by(treatment)
ttest egy_mgmt_score, by(treatment)

